package com.controller;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.model.SelectItem;

import org.apache.commons.lang3.StringUtils;

import com.dto.Item;
import com.dto.MoreSize;
import com.model.dao.ChildcatalogTable;
import com.model.dao.MasterCatalogTableDAO;
import com.model.dao.MastercatalogTable;

@ManagedBean(name = "itemsController")
@SessionScoped
public class ItemsController implements Serializable{

	/**
	 * 
	 */
	private static final long serialVersionUID = 6679513797056579455L;

	public List<Item> findAll() {
		// MastercatalogTable table = new MastercatalogTable();
		MasterCatalogTableDAO dao = new MasterCatalogTableDAO();
		List<MastercatalogTable> list = dao.findAll();
		List<Item> itemsList=converToLocal(list);
		return itemsList;
	}
	
	public List<Item> findByMenu(String level,Integer menuId){
		MasterCatalogTableDAO dao = new MasterCatalogTableDAO();
		List<MastercatalogTable> list = dao.findByMenu(level,menuId);
		List<Item> itemsList=converToLocal(list);
		return itemsList;
	}
	private List<Item> converToLocal(List<MastercatalogTable> list){
		List<Item> itemsList = new ArrayList<Item>();
		for (MastercatalogTable var : list) {
			Item item = new Item();
			item.setBrand(var.getItemBrand());
			item.setItemId(var.getItemId());
			item.setName(var.getItemName());
			List<MoreSize> childList = new ArrayList<MoreSize>();
			List<SelectItem> moreSizeSelectItems=new ArrayList<SelectItem>(); 
			int i = 0;
			for (ChildcatalogTable table : var.getChildcatalogTables()) {
				MoreSize moreSize = new MoreSize();
				moreSize.setItemImage(table.getChildItemImage());
				if (StringUtils.isNotBlank(table.getChildItemImagePath())) {
					moreSize.setImagePath(table.getChildItemImagePath());
				} else {
					moreSize.setImagePath("/images/upload/image_not_available.jpg");
				}
//				moreSize.setImagePath(table.getChildItemImagePath());
				moreSize.setMoreSizeItemId(table.getChildItemId());
				moreSize.setPrice(table.getChildItemPrice().doubleValue());
				moreSize.setSrp(table.getChildItemSrp().doubleValue());
				moreSize.setSizeName(table.getChildItemSize());
				if (null != table.getChildItemPrice()
						&& null != table.getChildItemSrp()) {
					moreSize.setOffer(table.getChildItemPrice().doubleValue()
							- table.getChildItemSrp().doubleValue());
				}
				childList.add(moreSize);
				moreSizeSelectItems.add(new SelectItem(table.getChildItemId(),table.getChildItemSize()+" - Rs."+ table.getChildItemSrp()));
				if (i == 0) {
					item.setOffer(moreSize.getOffer() + "");
					item.setPrice(moreSize.getPrice());
					item.setSrp(moreSize.getSrp());
					item.setSize(moreSize.getSizeName());
					item.setItemImage(moreSize.getItemImage());
					item.setItemImagePath(table.getChildItemImagePath());
					item.setMoreSizeItemId(moreSize.getMoreSizeItemId());
					i++;
				}
				item.setMoreSizesList(childList);
				item.setSizesList(moreSizeSelectItems);
			}
			itemsList.add(item);
		}
		return itemsList;
	}
	public List<Item> findByKeyword(String keyword) {
		// MastercatalogTable table = new MastercatalogTable();
		MasterCatalogTableDAO dao = new MasterCatalogTableDAO();
		List<MastercatalogTable> list = dao.findByKeyword(keyword);
		List<Item> itemsList = new ArrayList<Item>();
		for (MastercatalogTable var : list) {
			for (ChildcatalogTable table : var.getChildcatalogTables()) {
				Item item = new Item();
				item.setBrand(var.getItemBrand());
				item.setItemId(table.getChildItemId());
				item.setName(var.getItemName());
				item.setItemImage(table.getChildItemImage());
				if (StringUtils.isNotBlank(table.getChildItemImagePath())) {
					item.setItemImagePath(table.getChildItemImagePath());
				} else {
					item.setItemImagePath("/images/upload/image_not_available.jpg");
				}
				item.setSize(table.getChildItemSize());
				item.setMoreSizeItemId(table.getChildItemId());
				item.setPrice(table.getChildItemPrice().doubleValue());
				item.setSrp(table.getChildItemSrp().doubleValue());
				if (null != table.getChildItemPrice()
						&& null != table.getChildItemSrp()) {
					item.setOffer(table.getChildItemPrice().doubleValue()
							- table.getChildItemSrp().doubleValue() + "");
				}
				itemsList.add(item);
			}
		}
		return itemsList;
	}
	
}
